// pages/main/main.js
const app = getApp();
const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 选择商品
   */
  choose:function(){
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },

  /**
   * 分享朋友圈
   */
  friendCircle:function(){
    wx.navigateTo({
      url: '/pages/savePic/savePic',
    })
  },

//领礼物
  selectGift:function(res){
    wx.navigateTo({
      url: '/pages/getGift/getGift?orderId=' + this.data.order.orderId,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    let cid = app.globalData.cid
    let orderIngUrl = app.globalData.baseUrl + "orderIng"
    let data = {
      cid: cid
    }
    util.requestLoading(orderIngUrl, data, "GET").then(res => {
      console.log(res)
      app.globalData.orderList = res.data.orderList
      if (res.data.orderList != "" & res.data.orderList != null) {
        that.setData({
          status: 0,
          order: res.data.orderList[0]
        })
        that.countDown();
      } else {
        that.setData({
          status: 1
        })
      }
     
    })
  },

  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime() / 1000;
    let countDownArr = [];

    // 对结束时间进行处理渲染到页面
    let endTime = this.data.order.etime;
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = endTime - newTime  ;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {//活动已结束，全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj);
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({ countDownList: countDownArr })
    setTimeout(this.countDown, 1000);
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let name = this.data.order.giftName
    let orderId = this.data.order.orderId
    let cid = wx.getStorageSync('cid')
    let birthday = app.globalData.birthday
    let type = this.data.order.type
    return {
      title: '分享',
      path: '/pages/shareFriends/shareFriends?name=' + name + '&orderId=' + orderId + '&cid=' + cid + '&birthday=' + birthday + '&type=' + type
    }
  }
})